package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.CategoryReportMapper;
import com.qingcheng.pojo.order.CategoryReport;
import com.qingcheng.service.order.CategoryReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CategoryReportService.class)
@Transactional
public class CategoryReportServiceImpl implements CategoryReportService {

    @Autowired
    private CategoryReportMapper categoryReportMapper;

    @Override
    public List<CategoryReport> categoryReport(LocalDate date) {
        return categoryReportMapper.categoryReport(date);
    }

    @Override
    public void createDate() {
        LocalDate date = LocalDate.now().minusDays(1);
        List<CategoryReport> categoryReports = categoryReportMapper.categoryReport(date);
        for (CategoryReport categoryReport : categoryReports) {
            categoryReportMapper.insert(categoryReport);
        }
    }

    @Override
    public List<Map> category1Count(String beginDate, String endDate) {
        List<Map> maps = categoryReportMapper.category1Count(beginDate, endDate);
        Integer sumNum = 0;
        Integer sumMoney = 0;
        for (Map map : maps) {
            sumNum += (Integer) map.get("num");
            sumMoney += (Integer) map.get("money");
        }
        Double ratioNum = 0.0;
        Double ratioMoney = 0.0;
        for (Map map : maps) {
            ratioNum = (double)((Integer)map.get("num") / sumNum);
            ratioMoney = (double)((Integer)map.get("money") / sumMoney);
            map.put("ratioNum",ratioNum);
            map.put("ratioMoney",ratioMoney);
        }
        return maps;
    }
}
